// pages/cart/index.js
import {getSetting,chooseAddress,openSetting,showModel,showToast} from "../../utils/asyncWx.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    totalPrice:0,
    totalNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  //设置购物车状态

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address = wx.getStorageSync('address');
    //获取缓存中购物车的数据,确保类型正确
    let cart  = wx.getStorageSync('cart')||[];
    //1.计算全选,主要有一个返回false就不再循环,判断是否为空
    // const allChecked = cart.length?cart.every(v=>v.checked):false;
    //过滤后的购物车数组
    cart = cart.filter(v=>v.checked);
    let totalPrice = 0;
    let totalNum  = 0;
    cart.forEach(v=>{
      totalPrice+=v.num*v.goods_price;
      totalNum+=v.num;
  
    })  
    this.setData({
      cart,
      totalPrice,
      totalNum,
      address
    });
     wx.setStorageSync('cart',cart);
  },


  //
  handleOrderPay(){
    //判断缓存有没有token
    const token = wx.getStorageSync('token');
    //判断
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index',
      });
       return;
    }
  }


})